//比较含退格的字符串
//测试链接 https://leetcode.cn/problems/backspace-string-compare/
public class BackspaceCompare {
    public char[] stack1 = new char[200];
    public char[] stack2 = new char[200];

    public boolean backspaceCompare(String s, String t) {
        int pos1 = 0, pos2 = 0;

        char[] arr1 = s.toCharArray();
        char[] arr2 = t.toCharArray();

        for(int i = 0; i < arr1.length; i++){
            char ch = arr1[i];
            if(ch != '#'){
                stack1[pos1++] = ch;
            }else{ //此时是退格
                if(pos1 != 0) pos1--;  //删完就不能再删了
            }
        }

        for(int i = 0; i < arr2.length; i++){
            char ch = arr2[i];
            if(ch != '#'){
                stack2[pos2++] = ch;
            }else{ //此时是退格
                if(pos2 != 0) pos2--;  //删完就不能再删了
            }
        }

        String s1 = new String(stack1,0,pos1);
        String s2 = new String(stack2,0,pos2);

        return s1.equals(s2);
    }
}
